package com.woniu.defecttracking.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.woniu.defecttracking.commens.ComUtil;
import com.woniu.defecttracking.dto.MenusDto;
import com.woniu.defecttracking.entity.Menus;
import com.woniu.defecttracking.entity.RuleMenus;
import com.woniu.defecttracking.mapper.MenusMapper;
import com.woniu.defecttracking.service.MenusService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.woniu.defecttracking.service.RuleMenusService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author tiangle
 * @since 2021-12-31
 */
@Service
public class MenusServiceImpl extends ServiceImpl<MenusMapper, Menus> implements MenusService {

    @Resource
    private RuleMenusService ruleMenusService;

    /**
    * @作者: 倪西
    * @描述：查询出所有菜单以便授权
    * @时间：2022/1/5 11:32
    * @参数：[]
    * @返回：java.util.List<com.woniu.defecttracking.dto.MenusDto>
    **/
    @Override
    public List<MenusDto> findMenus() {
        List<Menus> menus = list();
        List<MenusDto> menusDtoList = ComUtil.copyList(menus, MenusDto.class);

        List<MenusDto> menusDtos = new ArrayList<>();
        Map<Integer, MenusDto> map = new HashMap<>();

        //将所有菜单用map集合进行分别保存，id为key，菜单对象为value
        for (MenusDto wmd:menusDtoList){
            if (wmd.getMenusParentid()==0){
                menusDtos.add(wmd);
            }
            map.put(wmd.getMenusId(),wmd);

        }

        //将拥有父菜单的子菜单加入其对应的父菜单的list集合中
        for (MenusDto wmd:menusDtoList){
            MenusDto menusParent = map.get(wmd.getMenusParentid());
            if (null!=menusParent){
                if (menusParent.getMenusDtos()==null){
                    menusParent.setMenusDtos(new ArrayList<>());
                }
                menusParent.getMenusDtos().add(wmd);
            }
        }
        return menusDtos;
    }
    /**
    * @作者: 倪西
    * @描述：通过rule查询对应的菜单
    * @时间：2022/1/7 21:40
    * @参数：[id]
    * @返回：java.util.List<java.lang.Integer>
    **/
    @Override
    public List<Integer> findMenusByRuleId(Integer id) {
        List<Menus> menus = list();
        List<MenusDto> menusDtoList = ComUtil.copyList(menus, MenusDto.class);

        List<MenusDto> menusDtos = new ArrayList<>();
        Map<Integer, MenusDto> map = new HashMap<>();

        for (MenusDto wmd:menusDtoList){
            if (wmd.getMenusParentid()==0){
                menusDtos.add(wmd);
            }
            map.put(wmd.getMenusId(),wmd);

        }

        for (MenusDto wmd:menusDtoList){
            MenusDto menusParent = map.get(wmd.getMenusParentid());
            if (null!=menusParent){
                if (menusParent.getMenusDtos()==null){
                    menusParent.setMenusDtos(new ArrayList<>());
                }
                menusParent.getMenusDtos().add(wmd);
            }
        }
        List<Integer> menusIds = ruleMenusService
                .list(new QueryWrapper<RuleMenus>().eq("rule_id", id))
                .stream().map(item -> item.getMenusId()).collect(Collectors.toList());
        //将父级菜单剔除集合，只剩下子节点菜单，取其id
        List<Menus> menusList = listByIds(menusIds);
        for (int i = 0; i < menusList.size(); i++) {
            if (menusList.get(i).getMenusParentid()==0){
                menusList.remove(i);
                i--;
            }
        }
        List<Integer> collect = menusList.stream()
                .map(item -> item.getMenusId()).collect(Collectors.toList());

        return collect;
    }

    /**
    * @作者: 倪西
    * @描述：service的实现类中通过账号ID查询该ID所属的菜单
    * @时间：2022/1/1 15:56
    * @参数：[id]
    * @返回：java.util.List<com.woniu.defecttracking.dto.MenusDto>
    **/
    @Override
    public List<MenusDto> findMenusByAccount(Integer id) {
        List<Menus> menus = this.baseMapper.selectMenusByAccount(id);
        List<MenusDto> menusDtos = ComUtil.copyList(menus, MenusDto.class);

        List<MenusDto> menusDtoList = new ArrayList<>();
        Map<Integer, MenusDto> map = new HashMap<>();

        for (MenusDto wmd:menusDtos){
            if (wmd.getMenusParentid()==0){
                menusDtoList.add(wmd);
            }
            map.put(wmd.getMenusId(),wmd);

        }

        for (MenusDto wmd:menusDtos){
            MenusDto menusParent = map.get(wmd.getMenusParentid());
            if (null!=menusParent){
                if (menusParent.getMenusDtos()==null){
                    menusParent.setMenusDtos(new ArrayList<>());
                }
                menusParent.getMenusDtos().add(wmd);
            }
        }
        return menusDtoList;
    }


}
